package com.glink.manage.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.BaseCommonUtils;
import com.glink.manage.domain.PatrolIntelligentCarouselGroup;
import com.glink.manage.domain.PatrolIntelligentEquip;
import com.glink.manage.domain.PatrolIntelligentEquipCarousel;
import com.glink.manage.dto.video.PatrolIntelligentEquipCarouselDTO;
import com.glink.manage.mapper.PatrolIntelligentEquipCarouselMapper;
import com.glink.manage.service.*;
import com.glink.manage.vo.video.PatrolIntelligentCarouselGroupVO;
import com.glink.manage.vo.video.PatrolIntelligentEquipCarouselVO;
import com.glink.manage.vo.video.PatrolIntelligentEquipSimpleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author  glink
 * @since 2024-07-19
 */
@Service
@Slf4j
public class PatrolIntelligentEquipCarouselServiceImpl extends ServiceImpl<PatrolIntelligentEquipCarouselMapper, PatrolIntelligentEquipCarousel> implements PatrolIntelligentEquipCarouselService {

    @Autowired
    private PatrolIntelligentCarouselGroupService intelligentCarouselGroupService;

    @Autowired
    private PatrolIntelligentEquipService patrolIntelligentEquipService;

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    private OrgService orgService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(PatrolIntelligentEquipCarouselDTO dto) {
        String orgId=null;
        try {
             orgId = BaseCommonUtils.extractOrdIdByToken();
        }catch (GeneralSimpleException e){
            log.info("单位信息获取异常",e);
            return false;
        }
        if (StringUtils.isBlank(orgId)) {
            log.info("单位信息不存在");
            return false;
        }
        PatrolIntelligentEquipCarousel carousel;
        if(StringUtils.isNotBlank(dto.getObjId())){
            carousel = this.getById(dto.getObjId());
        }else{
            carousel = new PatrolIntelligentEquipCarousel();
            carousel.setObjId(BaseCommonUtils.generateUUID());
            carousel.setProjectDepartmentObjId(orgId);
        }
        BeanUtil.copyProperties(dto,carousel, CopyOptions.create().setIgnoreNullValue(true));
        //轮播设备列表
        this.intelligentCarouselGroupService.lambdaUpdate().eq(PatrolIntelligentCarouselGroup::getCarouselObjId,carousel.getObjId()).remove();
        if(CollectionUtils.isNotEmpty(dto.getGroupDTOList())){
            List<PatrolIntelligentCarouselGroup> groupList = (dto.getGroupDTOList().stream().map(item ->{
                PatrolIntelligentCarouselGroup group = new PatrolIntelligentCarouselGroup();
                group.setObjId(BaseCommonUtils.generateUUID());
                group.setCarouselObjId(carousel.getObjId());
                BeanUtil.copyProperties(item,group,CopyOptions.create().setIgnoreNullValue(true));
                return group;
            }).collect(Collectors.toList()));
            if(CollectionUtils.isNotEmpty(groupList)){
                this.intelligentCarouselGroupService.saveOrUpdateBatch(groupList);
            }
        }
        return this.saveOrUpdate(carousel);
    }

    @Override
    public List<PatrolIntelligentEquipCarouselVO> findList() throws GeneralSimpleException {
        boolean headOffice = userService.checkHeadOffice();
        List<String> dataPermissionDepartmentOrgIdList =Lists.newArrayList();
        if (!headOffice){//当前人员没有总部管理员角色
            dataPermissionDepartmentOrgIdList = orgService.getDataPermissionDepartmentOrgId();
        }
        List<PatrolIntelligentEquipCarouselVO> vos = Lists.newArrayList();
        List<PatrolIntelligentEquipCarousel> list = this.lambdaQuery()
                .in(!headOffice && CollectionUtils.isNotEmpty(dataPermissionDepartmentOrgIdList),PatrolIntelligentEquipCarousel::getProjectDepartmentObjId,dataPermissionDepartmentOrgIdList)
                .orderByAsc(PatrolIntelligentEquipCarousel::getCreateTime).list();
        if(CollectionUtils.isNotEmpty(list)){
            for(PatrolIntelligentEquipCarousel p : list){
                PatrolIntelligentEquipCarouselVO vo = new PatrolIntelligentEquipCarouselVO();
                BeanUtil.copyProperties(p,vo);
                //轮播设备列表
                findGroup(vo);
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public PatrolIntelligentEquipCarouselVO findById(String objId) {
        PatrolIntelligentEquipCarouselVO vo = new PatrolIntelligentEquipCarouselVO();

        PatrolIntelligentEquipCarousel carousel = this.getById(objId);
        BeanUtil.copyProperties(carousel,vo);
        //轮播设备列表
        findGroup(vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String objId) {
        boolean status = this.lambdaUpdate().eq(PatrolIntelligentEquipCarousel::getObjId, objId).remove();
        if (status){
             status = intelligentCarouselGroupService.lambdaUpdate().eq(PatrolIntelligentCarouselGroup::getCarouselObjId, objId).remove();
        }
        return status;
    }

    /**
     * //轮播设备列表
     * @param vo
     */
    public void findGroup(PatrolIntelligentEquipCarouselVO vo){
        List<PatrolIntelligentCarouselGroup> groupList = this.intelligentCarouselGroupService.lambdaQuery()
                .eq(PatrolIntelligentCarouselGroup::getCarouselObjId,vo.getObjId()).orderByAsc(PatrolIntelligentCarouselGroup::getCreateTime).list();
        if(CollectionUtils.isNotEmpty(groupList)){
            vo.setGroupVOList(groupList.stream().map(item -> {
                PatrolIntelligentCarouselGroupVO groupVO = new PatrolIntelligentCarouselGroupVO();
                BeanUtil.copyProperties(item, groupVO);
                //查询摄像机信息
                groupVO.setEquipVOList(patrolIntelligentEquipService.findByPatrolIdIn(item.getPatrolEquipId()));
                return groupVO;
            }).collect(Collectors.toList()));
        }
    }
}
